package wang.aweb.childcar.controller.agentApi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.filter.AgentFilter;
import wang.aweb.childcar.service.DeviceService;
import wang.aweb.childcar.service.MemberService;
import wang.aweb.childcar.service.OrderService;
import wang.aweb.childcar.util.MonthDataUtils;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/agentApi/tongji/")
public class TongjiController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @PostMapping("getTjData")
    public ApiRestResponse getTjData(){
        Integer agentId=AgentFilter.agentThreadLocal.get().getId();
        Map<String,Object> resMap=new HashMap<>();
        Integer memberCount=memberService.getAgentUserCount(agentId,null,null);
        resMap.put("memberCount",memberCount);
        Integer deviceCount=deviceService.getAgentDeviceCount(agentId,null,null);
        resMap.put("deviceCount",deviceCount);
        Integer orderCount=orderService.getAgentOrderCount(agentId,null,null);
        resMap.put("orderCount",orderCount);
        List<Map<String,Object>> tableData=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("MM");
        Calendar c=Calendar.getInstance();
        for(int i=0;i<6;i++){

            Map<String,Object> itemMap=new HashMap<>();
            Integer month=Integer.valueOf(sdf.format(new Date()))-i;
            String monthStr=month+"月";
            String MMstr="";
            if(month<10){
                MMstr="0"+month;
            }else{
                MMstr=month.toString();
            }
            SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-"+MMstr+"-01 00:00:00");
            Date starttime=null;
            Date endtime=null;
            try {
                starttime=sdf1.parse(sdf2.format(new Date()));
                endtime=sdf1.parse(MonthDataUtils.getMonthEnd(sdf2.format(new Date())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            itemMap.put("month",monthStr);
            Integer itemDeviceCount=deviceService.getAgentDeviceCount(agentId,starttime,endtime);
            itemMap.put("deviceCount",itemDeviceCount);
            Integer itemMemberCount=memberService.getAgentUserCount(agentId,starttime,endtime);
            itemMap.put("memberCount",itemMemberCount);
            Integer itemOrderCount=orderService.getAgentOrderCount(agentId,starttime,endtime);
            itemMap.put("orderCount",itemOrderCount);
            tableData.add(itemMap);
        }
        resMap.put("tableData",tableData);
        return ApiRestResponse.success(resMap);
    }
}
